//
//  LGSearchResultVC.m
//  ALS3.0
//
//  Created by guomianxiang on 2017/4/9.
//  Copyright © 2017年 guomianxiang. All rights reserved.
//

#import "LGSearchResultVC.h"
#import "LGDoorInfoViewController.h"
//#import "LGtools.h"
#import "LGDCTableHead.h"
#import <objc/runtime.h>
//#import "UIView+LG.h"
#import "ALSHeader.h"

#define k_sectionHeadViewHeight 35

@interface LGSearchResultVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *presentationLab;
@property (strong, nonatomic) NSMutableArray <UIView *>* sectionHeadViewArr;
@property (strong, nonatomic) NSArray <NSArray <LGDCCellContent *>*>* cellContentArr;
@end

@implementation LGSearchResultVC

- (void)viewDidLoad {
    [super viewDidLoad];
    k_registerNib(self.tableView);
//    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}


- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    
//    [searchController.searchBar sizeToFit];

    NSString *searchStr = searchController.searchBar.text;
//    NSLog(@"frame - %@",NSStringFromCGRect(searchController.searchBar.frame));
//    
//    NSLog(@"father : %@",searchController.searchBar.superview);
//
//    NSLog(@"father : %@",searchController.searchBar.superview.superview);
    
//    NSLog(@"hidden : %i",self.view.hidden);

    if (searchStr.length == 0) {
        return;
    }

    [DCMI searchDoorWithAndRecommend:searchStr reply:^(NSArray<LGDoor *> * _Nullable resultArr, NSArray<LGDoor *> * _Nullable recommendArr) {
        NSMutableArray <NSArray <LGDoor *>*>*doors = [NSMutableArray array];
        
        self.sectionHeadViewArr = [NSMutableArray array];
        if (resultArr.count != 0) {
            [doors addObject:resultArr];
            [self.sectionHeadViewArr addObject:[self tableViewSectionHeadViewWithTitle:@"搜索结果"]];
        }
        
        if (recommendArr.count != 0) {
            [doors addObject:recommendArr];
            [self.sectionHeadViewArr addObject:[self tableViewSectionHeadViewWithTitle:@"推荐结果"]];
        }
        
        self.cellContentArr = [LGDCCellContent transfromSearchResultCellContentArrWithDoorsArr:doors];
        
        if (self.cellContentArr.count == 0) {
            self.presentationLab.hidden = NO;
        }else {
            self.presentationLab.hidden = YES;
        }
        
        [self.tableView reloadData];
    }];
}

#pragma mark - tableviewdelegate

k_tableViewDataSourceAndCellHeight(cellContentArr, nil)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    
    
    if ([self.delegate respondsToSelector:@selector(didSelectDoor:)]) {
        LGDCCellContent *content = self.cellContentArr[indexPath.section][indexPath.row];
        
        [self.delegate didSelectDoor:content.door];
        
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    NSLog(@"%@",NSStringFromUIEdgeInsets(self.tableView.contentInset));
    self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);

    return k_sectionHeadViewHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 8;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.sectionHeadViewArr[section];
}

- (UIView *)tableViewSectionHeadViewWithTitle:(NSString *)title {
    UIView *headView = [[UIView alloc]init];
    headView.backgroundColor = [UIColor clearColor];
    headView.frame = CGRectMake(0, 0, kScreen_W, k_sectionHeadViewHeight);

    UILabel *textLab = [[UILabel alloc]init];
//    textLab.textColor = kGrayColor(170);
    textLab.textColor = kALSHightBlueColor;

    textLab.font = [UIFont systemFontOfSize:12];
    textLab.text = title;
    [textLab sizeToFit];
    textLab.x = 12;
    textLab.y = headView.height - textLab.height - 8;
    [headView addSubview:textLab];
    return headView;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    LGDoorInfoViewController *doorInfoVC = segue.destinationViewController;
    doorInfoVC.door = sender;

}

- (void)setClean:(BOOL)clean {
    if(clean) {
        
        self.view.hidden = YES;
//        self.cellContentArr = [NSMutableArray array];
//        [self.tableView reloadData];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([self.parentViewController isKindOfClass:[UISearchController class]]) {
        UISearchController *searchC = (UISearchController *)self.parentViewController;
        [searchC.searchBar resignFirstResponder];
    }
}

@end


//- (void)presentSearchController:(UISearchController *)searchController {
//    
//}
//
//- (void)willPresentSearchController:(UISearchController *)searchController {
//    
//    UISearchBar *searchBar = searchController.searchBar;
//    
//    CGFloat value = 0.95;
//    searchBar.barTintColor = [UIColor colorWithRed:value green:value blue:value alpha:1];
//    
//    UIButton *cancelbtn = [searchBar valueForKey:@"cancelButton"];
//    [cancelbtn setTitleColor:kColorFromRGB(0x3FEDEB) forState:(UIControlStateNormal)];
//    [cancelbtn setTitleColor:kColorFromRGB(0x3FEDEB) forState:(UIControlStateHighlighted)];
//    [cancelbtn setTitleColor:kColorFromRGB(0x3FEDEB) forState:(UIControlStateSelected)];
//    
//    
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
//}
//
//- (void)didPresentSearchController:(UISearchController *)searchController {
//    
//    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
//    
//    UIVisualEffectView *visualView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
//    visualView.frame = searchController.view.bounds;
//    visualView.userInteractionEnabled = NO;
//    [searchController.view addSubview:visualView];
//    
//}
//
//- (void)willDismissSearchController:(UISearchController *)searchController {
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
//    
//}
//
//- (void)didDismissSearchController:(UISearchController *)searchController {
//    CGFloat value = 0.85;
//    searchController.searchBar.barTintColor = [UIColor colorWithRed:value green:value blue:value alpha:1];
//    
//}


//- (void)printIvar:(Class)class {
//    // count记录变量的数量IVar是runtime声明的一个宏
//    unsigned int count = 0;
//    // 获取类的所有属性变量
//    Ivar *menbers = class_copyIvarList(class, &count);
//    NSLog(@"count : %i",count);
//    for (int i = 0; i < count; i++) {
//        Ivar var = menbers[i];
//        // 将IVar变量转化为字符串,这里获得了属性名和类型
//        const char *memberName = ivar_getName(var);
//        const char *memberType = ivar_getTypeEncoding(var);
//        NSLog(@"%s----%s", memberName, memberType);
//        //并不能打印父类属性
//    }
//}
//


//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    return self.cellContentArr.count;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return self.cellContentArr[section].count;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//
//    LGDCCellContent *content = self.cellContentArr[indexPath.section][indexPath.row];
//    content.indexPath = indexPath;
//
//    LGDCBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(content.cellClass)];
//    cell.content = content;
//
//    return cell;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return self.cellContentArr[indexPath.section][indexPath.row].cellHeight;
//}
